package org.metasketch.demo.diagrams.OFD;

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import org.metasketch.demo.model.ActorRole;
import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.CustomLabel;
import org.metasketch.diagram.javafx.graphics.Resizableness;
import org.metasketch.graphics.GraphicNode;

public class ActorRoleNode extends BaseGraphic {

	CustomLabel nameLabel = new CustomLabel("Untitled");
	CustomLabel identifierLabel = new CustomLabel("Untitled");
	Rectangle rect;// = new Rectangle();

	Color lineColor = Color.BLACK;
	Color lightBackColor = Color.web("f5f5f5");
	Color darkBackColor = Color.web("808080");

	public ActorRoleNode() {
		super(new GraphicNode(0, 0, 80, 160));
		// setup(x, y, width, height);

		// setResizable(Resizableness.HORIZONTAL);
	}

	@Override
	protected void continueSetup() {

		setupGraphic(graphicNode.getX(), graphicNode.getY(),
				graphicNode.getWidth(), graphicNode.getHeight());

	}

	private void setupGraphic(int x, int y, int width, int height) {

		this.width = width;
		this.height = height;

		setX(x);
		setY(y);

		rect = new Rectangle(1, 1, width, height);
		rect.setStroke(lineColor);
		rect.setStrokeWidth(2);
		rect.setFill(lightBackColor);

		// rect.setStyle("-fx-stroke: green;  -fx-stroke-width: 5;  -fx-stroke-type: outside; -fx-stroke-dash-array: 5,5; -fx-opacity: 0.5;");
		// rect.setD

		identifierLabel.setGraphic(this);
		identifierLabel.setTextVerticalTop();
		identifierLabel.setVerticallyCentered(false);
		identifierLabel.setFieldNameEditing("identifier", true);

		nameLabel.setGraphic(this);
		nameLabel.setFieldNameEditing("name", true);

		// identifierLabel.setReadScript("semantic.identifier");
		// nameLabel.setReadScript("semantic.name");

		// identifierLabel.setWriteScript("semantic.identifier = text;");
		// nameLabel.setWriteScript("semantic.name = text;");

		// nameLabel.setReadScript(" if(semantic.initiates.size()==0) return semantic.name; else { return semantic.name + ' -> ' + semantic.initiates[0].getIdentifier();   }  ");
		// nameLabel.setEditable(false);

		getChildren().addAll(rect, nameLabel, identifierLabel);
	}

	public void setComposite(boolean composite) {
		rect.setFill(composite ? darkBackColor : lightBackColor);
	}

	@Override
	protected void resizeGraphic() {
		rect.setWidth(width);
		rect.setHeight(height);

		identifierLabel.resize();
		nameLabel.resize();

	}

	@Override
	protected void refreshModelGraphic() {
		ActorRole role = (ActorRole) semantic;

		// nameLabel.setText(role.getName());
		// identifierLabel.setText(role.getIdentifier());

		nameLabel.refreshModelGraphic();
		identifierLabel.refreshModelGraphic();

		// TODO find a way to do listeners to this...mega crap

		if (role.isComposite() || !role.getContainedActorRoles().isEmpty()
				|| role.getExecutes().size() > 1)
			rect.setStyle("-fx-stroke: black;  -fx-stroke-type: outside;  -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 2 4 2;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;");
		else
			rect.setStyle("-fx-stroke: black; -fx-stroke-width: 2;  ");

		if (role.getAssignedTo() == null)
			rect.setFill(darkBackColor);
		else
			rect.setFill(lightBackColor);

	}

}
